package org.jeecg.modules.villageCadreAssist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.villageCadreAssist.entity.VcaFarmland;
import org.jeecg.modules.villageCadreAssist.entity.VcaFarmlandRent;
import org.jeecg.modules.villageCadreAssist.mapper.VcaFarmlandRentMapper;
import org.jeecg.modules.villageCadreAssist.service.IVcaFarmlandRentService;
import org.jeecg.modules.villageCadreAssist.service.IVcaFarmlandService;
import org.jeecg.modules.villageCadreAssist.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: vca_farmland_rent
 * @Author: jeecg-boot
 * @Date:   2022-04-10
 * @Version: V1.0
 */
@Service
public class VcaFarmlandRentServiceImpl extends ServiceImpl<VcaFarmlandRentMapper, VcaFarmlandRent> implements IVcaFarmlandRentService {

    @Autowired
    private IVcaFarmlandService vcaFarmlandService;


    public IPage<VcaFarmlandRent> queryPageList(Page<VcaFarmlandRent> page, QueryWrapper<VcaFarmlandRent> queryWrapper, String orgCode){
        return baseMapper.queryPageList(page,queryWrapper,orgCode);
    }

    @Transactional
    public void add(VcaFarmlandRent vcaFarmlandRent){
        Assert.isTrue(!vcaFarmlandRent.getRentId().equals(vcaFarmlandRent.getOwnId()),"不能出租给本人");

        Assert.isTrue(BigDecimal.ZERO.compareTo(vcaFarmlandRent.getRentArea())!=1,"出租面积不正确");
        VcaFarmland own = vcaFarmlandService.getById(vcaFarmlandRent.getOwnId());
        Assert.isTrue(vcaFarmlandRent.getRentArea().compareTo(own.getTillageArea())<1,"出租面积不能大于可耕地面积");

        VcaFarmland rent = vcaFarmlandService.getById(vcaFarmlandRent.getRentId());

        VcaFarmland vcaFarmland = new VcaFarmland();
        vcaFarmland.setId(own.getId());
        vcaFarmland.setTillageArea(own.getTillageArea().subtract(vcaFarmlandRent.getRentArea()));
        vcaFarmlandService.updateById(vcaFarmland);

        vcaFarmland.setId(rent.getId());
        vcaFarmland.setTillageArea(rent.getTillageArea().add(vcaFarmlandRent.getRentArea()));
        vcaFarmlandService.updateById(vcaFarmland);

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        vcaFarmlandRent.setCreateBy(user.getUsername());
        vcaFarmlandRent.setCreateTime(new Date());

        VcaFarmlandRent farmlandRent = super.getOne(new LambdaQueryWrapper<VcaFarmlandRent>()
                .eq(VcaFarmlandRent::getOwnId, vcaFarmlandRent.getOwnId())
                .eq(VcaFarmlandRent::getRentId, vcaFarmlandRent.getRentId()));
        if(farmlandRent==null){
            super.save(vcaFarmlandRent);
        }else{
            farmlandRent.setRentArea(farmlandRent.getRentArea().add(vcaFarmlandRent.getRentArea()));
            super.updateById(farmlandRent);
        }
    }

    @Transactional
    public void delete(String id){
        VcaFarmlandRent vcaFarmlandRent = super.getById(id);

        VcaFarmland own = vcaFarmlandService.getById(vcaFarmlandRent.getOwnId());
        VcaFarmland rent = vcaFarmlandService.getById(vcaFarmlandRent.getRentId());

        VcaFarmland vcaFarmland = new VcaFarmland();
        vcaFarmland.setId(own.getId());
        vcaFarmland.setTillageArea(own.getTillageArea().add(vcaFarmlandRent.getRentArea()));
        vcaFarmlandService.updateById(vcaFarmland);

        vcaFarmland.setId(rent.getId());
        vcaFarmland.setTillageArea(rent.getTillageArea().subtract(vcaFarmlandRent.getRentArea()));
        vcaFarmlandService.updateById(vcaFarmland);

        super.removeById(id);
    }
}
